const fileToDataURL = (file) => {
	return new Promise((resolve) => {
		const reader = new FileReader()
		reader.onloadend = (e) => resolve(e.target.result)
		reader.readAsDataURL(file)
	})
}

const dataURLToImage = (dataURL) => {
	return new Promise((resolve) => {
		const img = new Image()
		img.onload = () => resolve(img)
		img.src = dataURL
	})
}
const canvastoFile = (canvas, type, quality) => {
	return new Promise((resolve) =>
		canvas.toBlob((blob) => resolve(blob), type, quality)
	)
}
/**
 * 图片压缩方法
 * @param {Object}  file 图片文件
 * @param {String} type 想压缩成的文件类型
 * @param {Nubmber} quality 压缩质量参数
 * @returns 压缩后的新图片
 */
const compressionFile = async (file, type = 'image/jpeg', quality = 0.5) => {
	const fileName = file.name
	const canvas = document.createElement('canvas')
	const context = canvas.getContext('2d')
	const base64 = await fileToDataURL(file)
	const img = await dataURLToImage(base64)
	canvas.width = img.width
	canvas.height = img.height
	context.clearRect(0, 0, img.width, img.height)
	context.drawImage(img, 0, 0, img.width, img.height)
	const blob = await canvastoFile(canvas, type, quality) // quality:0.5可根据实际情况计算
	const newFile = new File([blob], fileName, {
		type: type
	})
	canvas.remove()
	return newFile
}

export default compressionFile
